package com.edu.chapter07;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
/*
 * AnimationDrawable
 * 是 Android 中用于实现帧动画（Frame Animation）的核心类，
 * 继承自 DrawableContainer（最终基类为 Drawable）。
 * 其本质是一个可绘制资源（Drawable），而非动画类（如 ViewAnimation 或 PropertyAnimation），
 * 因此需通过 Drawable 的接口操作动画。
 */
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

import java.util.Objects;

public class FrameAnimActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame_anim);
        ImageView imageView = findViewById(R.id.iv_run_anim);
//        imageView.setImageResource(R.drawable.frame_run_anim);
//
//        AnimationDrawable animationDrawable=(AnimationDrawable) imageView.getDrawable();
//        animationDrawable.start();

        AnimationDrawable animationDrawable = new AnimationDrawable();
        animationDrawable.setOneShot(false);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.a)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.b)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.c)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.d)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.e)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.f)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.g)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.h)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.i)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.j)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.k)), 200);
        animationDrawable.addFrame(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.l)), 200);
        imageView.setImageDrawable(animationDrawable);
        animationDrawable.start();
    }
}